"""
设备管理权限初始化脚本
用于添加设备管理相关权限到系统中
"""
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from common.database import DatabaseManager
from common.config import settings


def init_device_permissions():
    """初始化设备管理权限"""
    db = DatabaseManager()

    try:
        # 检查是否已存在设备管理权限
        check_sql = "SELECT id FROM base_permission WHERE perm_code = %s"
        existing = db.execute_query(check_sql, ("device:manage",))

        if existing:
            print("设备管理权限已存在，跳过初始化")
        else:
            # 插入设备管理权限
            insert_sql = """
                INSERT INTO base_permission (perm_code, perm_name, type, parent_id, path, level, sort)
                VALUES (%s, %s, %s, %s, %s, %s, %s)
            """

            db.execute_query(insert_sql, (
                "device:manage",
                "设备管理",
                "API",
                0,
                "/api/v1/devices",
                1,
                50
            ), commit=True)

            print("设备管理权限初始化成功")

        # 检查是否已存在遥测数据权限
        check_sql = "SELECT id FROM base_permission WHERE perm_code = %s"
        existing = db.execute_query(check_sql, ("telemetry:view",))

        if existing:
            print("遥测数据权限已存在，跳过初始化")
        else:
            # 插入遥测数据权限
            insert_sql = """
                INSERT INTO base_permission (perm_code, perm_name, type, parent_id, path, level, sort)
                VALUES (%s, %s, %s, %s, %s, %s, %s)
            """

            db.execute_query(insert_sql, (
                "telemetry:view",
                "遥测数据查看",
                "API",
                0,
                "/api/v1/telemetry",
                1,
                51
            ), commit=True)

            print("遥测数据查看权限初始化成功")

        # 检查是否已存在遥测数据管理权限
        check_sql = "SELECT id FROM base_permission WHERE perm_code = %s"
        existing = db.execute_query(check_sql, ("telemetry:manage",))

        if existing:
            print("遥测数据管理权限已存在，跳过初始化")
        else:
            # 插入遥测数据管理权限
            insert_sql = """
                INSERT INTO base_permission (perm_code, perm_name, type, parent_id, path, level, sort)
                VALUES (%s, %s, %s, %s, %s, %s, %s)
            """

            db.execute_query(insert_sql, (
                "telemetry:manage",
                "遥测数据管理",
                "API",
                0,
                "/api/v1/telemetry",
                1,
                52
            ), commit=True)

            print("遥测数据管理权限初始化成功")

        # 检查是否已存在工单查看权限
        check_sql = "SELECT id FROM base_permission WHERE perm_code = %s"
        existing = db.execute_query(check_sql, ("workorder:view",))

        if existing:
            print("工单查看权限已存在，跳过初始化")
        else:
            # 插入工单查看权限
            insert_sql = """
                INSERT INTO base_permission (perm_code, perm_name, type, parent_id, path, level, sort)
                VALUES (%s, %s, %s, %s, %s, %s, %s)
            """

            db.execute_query(insert_sql, (
                "workorder:view",
                "工单查看",
                "API",
                0,
                "/api/v1/work-orders",
                1,
                53
            ), commit=True)

            print("工单查看权限初始化成功")

        # 检查是否已存在工单管理权限
        check_sql = "SELECT id FROM base_permission WHERE perm_code = %s"
        existing = db.execute_query(check_sql, ("workorder:manage",))

        if existing:
            print("工单管理权限已存在，跳过初始化")
        else:
            # 插入工单管理权限
            insert_sql = """
                INSERT INTO base_permission (perm_code, perm_name, type, parent_id, path, level, sort)
                VALUES (%s, %s, %s, %s, %s, %s, %s)
            """

            db.execute_query(insert_sql, (
                "workorder:manage",
                "工单管理",
                "API",
                0,
                "/api/v1/work-orders",
                1,
                54
            ), commit=True)

            print("工单管理权限初始化成功")

        # 检查是否已存在设备事件查看权限
        check_sql = "SELECT id FROM base_permission WHERE perm_code = %s"
        existing = db.execute_query(check_sql, ("device:event:view",))

        if existing:
            print("设备事件查看权限已存在，跳过初始化")
        else:
            # 插入设备事件查看权限
            insert_sql = """
                INSERT INTO base_permission (perm_code, perm_name, type, parent_id, path, level, sort)
                VALUES (%s, %s, %s, %s, %s, %s, %s)
            """

            db.execute_query(insert_sql, (
                "device:event:view",
                "设备事件查看",
                "API",
                0,
                "/api/v1/device-events",
                1,
                55
            ), commit=True)

            print("设备事件查看权限初始化成功")

    except Exception as e:
        print(f"初始化权限失败: {e}")
        raise
    finally:
        db.close()


if __name__ == "__main__":
    init_device_permissions()